package codeHuffman2;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.PriorityQueue;

public class Test {


	public static void main(String[] args) throws IOException {

/*-------------------------------TEST UNITAIRE-----------------------------------------------------*/
		System.out.println("-------------TEST UNITAIRE------------------------------"+"\n");

		//test lecture file et HashMap de nombre d'occurence
		System.out.println("-------TEST test lirefichier----------");
		HashMap<Character, Integer> nombreOccurence = CompressionFile.lireFichier("testUnitaire.txt");
		System.out.println(nombreOccurence);

		//test de PriorityQueue	
		System.out.println("-------TEST TriPriorityQueue----------");	
		PriorityQueue<Arbre> pq1=CompressionFile.triPQ(nombreOccurence);
		Iterator<Arbre> c=pq1.iterator();
		String s = "";
		while(c.hasNext()){
			Noeud d=pq1.poll().racine;
			s=s+(" "+d.caractere+"="+d.frequence);
		}
		System.out.println(s);

		//test ConstructionHuffmanArbre
		System.out.println("-------TEST ConstructionHuffmanArbre----------");
		PriorityQueue<Arbre> pq=CompressionFile.triPQ(nombreOccurence);
		Arbre arbreHuffman = CompressionFile.constructionArbreHuffman(pq);
		System.out.println("Infixe (recursif):   " + arbreHuffman.affichageInfixeRecursif());

		//test codageCarectere
		System.out.println("-------test CodageCaractere----------");
		String code = "";
		HashMap<Character,String> dictionnaire = new HashMap<Character,String>();
		dictionnaire=CompressionFile.codageCaractere(arbreHuffman.racine,code,dictionnaire);
		System.out.println(dictionnaire);

		//test compression
		System.out.println("-------Test Compression-----------");
		System.out.println("Compresser...");
		CompressionFile.compression(dictionnaire,"testUnitaire.txt","testUnitaire_Compresse.txt");
		System.out.println("Ok");

		//Decompression 
		System.out.println("--------Decompression----------");
		System.out.println("Decompresser...");
		Decompresson.decompression(arbreHuffman,"testUnitaire_Compresse.txt","testUnitaire_Decompresse.txt");
		System.out.println("Ok");

		/*-------------------------------TEST REALISE-----------------------------------------------------*/
		System.out.println("-------------TEST REALISE------------------------------"+"\n");
		
		//Test Bible
		System.out.println("TEST Test Bible");
		long startTime = System.currentTimeMillis();
		new CompressionFile("bible.txt", "bible_Compresse.txt");
		long endTime = System.currentTimeMillis();
	    System.out.println("Temps de compression :" + (endTime-startTime) + "ms");
	    
	    startTime = System.currentTimeMillis();
		new Decompresson("bible.txt","bible_Compresse.txt","bible_Decompresse.txt");
		endTime = System.currentTimeMillis();
	    System.out.println("Temps de decompression :" + (endTime-startTime) + "ms");
	    
		//Test ANSI CODE
		System.out.println("TEST ANSI CODE");
		startTime = System.nanoTime()/1000000;
		new CompressionFile("testAscii.txt", "testAscii_Compresse.txt");
		endTime = System.nanoTime()/1000000;
		System.out.println("Temps de compression :" + (endTime-startTime) + "ms");
		
		startTime = System.nanoTime()/1000000;
		new Decompresson("testAscii.txt","testAscii_Compresse.txt","testAscii_Decompresse.txt");
		endTime = System.nanoTime()/1000000;
	    System.out.println("Temps de decompression :" + (endTime-startTime) + "ms");
	    
		//Test Caractere Unique
		System.out.println("TEST Caractere Unique");
		startTime = System.nanoTime()/1000000;
		new CompressionFile("testCaractereUnique.txt", "testCaractereUnique_Compresse.txt");
		endTime = System.nanoTime()/1000000;
		System.out.println("Temps de compression :" + (endTime-startTime) + "ms");
		
		startTime = System.nanoTime()/1000000;
		new Decompresson("testCaractereUnique.txt","testCaractereUnique_Compresse.txt","testCaractereUnique_Decompresse.txt");
		endTime = System.nanoTime()/1000000;
	    System.out.println("Temps de decompression :" + (endTime-startTime) + "ms");
	}
}
